// JavaScript Document


$(document).ready(function(e) {	
////////////////// new credit commitment calculation////////////////////////
	var h1payment=$("#h1payment").text(); h1payment=toNumber(h1payment);
	var h2payment=$("#h2payment").text(); h2payment=toNumber(h2payment);
	
	var v1value=$("#vehicleValue").text(); v1value=toNumber(v1value); //alert (v1value);
	var v2value=$("#vehicle2Value").text(); v2value=toNumber(v2value);
	var v1payment=$("#v1payment").text(); v1payment=toNumber(v1payment); //alert (v1payment);
	var v2payment=$("#v2payment").text(); v2payment=toNumber(v2payment);
	
	var oa1payment=$("#otherh1payment").text(); oa1payment=toNumber(oa1payment);
	var oa2payment=$("#otherh2payment").text(); oa2payment=toNumber(oa2payment);
	
	var p1owing=$("#p1owing").text(); p1owing=toNumber(p1owing);
	var p2owing=$("#p2owing").text(); p2owing=toNumber(p2owing);
	var c1owing=$("#c1owing").text(); c1owing=toNumber(c1owing);
	var c2owing=$("#c2owing").text(); c2owing=toNumber(c2owing);
	
	var p1payment=$("#p1payment").text(); p1payment=toNumber(p1payment);
	var p2payment=$("#p2payment").text(); p2payment=toNumber(p2payment);
	var c1payment=$("#c1payment").text(); c1payment=toNumber(c1payment);
	var c2payment=$("#c2payment").text(); c2payment=toNumber(c2payment);
	
	var o1payment=$("#other1payment").text(); o1payment=toNumber(o1payment);
	var o2payment=$("#other2payment").text(); o2payment=toNumber(o2payment);
	
	var wrongVehicleRepayment=0;
	if(v1payment > v1value/2){
		 v1payment=parseInt(v1value/24);
		 wrongVehicleRepayment=1;
	}
	if(v2payment > v2value/2){
		 v2payment=parseInt(v2value/24);
		 wrongVehicleRepayment=1;
	}
	
	
	var wrongLiability=0;
	if(p1owing > 1000000){
		p1payment=0;
		wrongLiability=1;
	}
	else{
		if(p1payment > p1owing/2){
			p1payment=parseInt(p1owing/24);
			wrongLiability=1;
		}
	}
	
	if(p2owing > 1000000){
		p2payment=0;
		wrongLiability=1;
	}
	else{
		if(p2payment > p2owing/2){
			p2payment=parseInt(p2owing/24);
			wrongLiability=1;
		}
	}
	
	if(c1owing > 1000000){
		c1payment=0;
		wrongLiability=1;
	}
	else{
		if(c1payment > c1owing/2){
			c1payment=parseInt(c1owing/24);
			wrongLiability=1;
		}
	}
	
	if(c2owing > 1000000){
		c2payment=0;
		wrongLiability=1;
	}
	else{
		if(c2payment > c2owing/2){
			c2payment=parseInt(c2owing/24);
			wrongLiability=1;
		}
	}
	
	var totalpayment=h1payment+h2payment+v1payment+v2payment+oa1payment+oa2payment+p1payment+p2payment+c1payment+c2payment+o1payment+o2payment;
	
	if(wrongLiability==1)
		$("#diReason").append(" Unreasonable liabilities");
	//alert (totalpayment);
	if(wrongVehicleRepayment==1)
		$("#diReason").append(" Unreasonable motor vehicle repayment.");









//////////////////// calculate total expenses ////////////////////////////////
	var r=$("#rent").text();r = r.replace(/\,/g,''); if(r=="") r=0; else r=parseInt(r);
	var i=$("#totalIncome").text();  i = i.replace(/\,/g,''); i=parseInt(i);
	var l=$("#livingExpenses").text(); l = l.replace(/\,/g,''); l=parseInt(l);
	var c=totalpayment;  $("#newcc").text(c);
	var hv=$("#homeValue").text(); hv = hv.replace(/\,/g,''); hv=parseInt(hv);
	
	var nod1=$("#nod1").text();			//use the higher number of dependent
	var nod2=$("#nod2").text();
	if(nod2>nod1)
		$("#nod1").text(nod2);
	
	if(hv!="" && hv>0)  //if client has property, no need to calculate rent
		r=0;
	
    if($("#title2").text()==""){   
										//1 applicant
		if($("#nod1").text()==0){		//number of dependants
			var lowest=r+1000;	
		}
		if($("#nod1").text()==1){	
			var lowest=r+1353;
		}
		if($("#nod1").text()==2){
			var lowest=r+1707;		
		}
		if($("#nod1").text()==3){	
			var lowest=r+2061;		
		}
		if($("#nod1").text()>=4){		
			var lowest=r+2414;		
		}
		if(lowest>l)
			$("#livingExpenses").text(lowest);			
	}
	else{								//2 applicants	
			
		if($("#nod1").text()==0){		//number of dependants
			var lowest=r+1430;	
		}
		if($("#nod1").text()==1){		
			var lowest=r+1781;	
		}
		if($("#nod1").text()==2){		
			var lowest=r+2135;	
		}
		if($("#nod1").text()==3){		
			var lowest=r+2489;	
		}
		if($("#nod1").text()>=4){		
			var lowest=r+2842;	
		}	
		if(lowest>l)
			$("#livingExpenses").text(lowest);			
	}
	
	
/////////////////////////////////////////calculate disposable income////////////////////////////////////////
var l=$("#livingExpenses").text(); l = l.replace(/\,/g,''); 
$("#newle").text(l);
var totalexpenses = parseInt(l) + parseInt(c);  $("#newte").text(totalexpenses);
var dincome = parseInt(i)-totalexpenses;
$("#dIncomeResult").text(dincome);

//set disposable state and text color
if(dincome<=(-1000)){
	$("#dIncomeTitle").html("Disposable Income - <span style='color:#caca00'>Manual Check Required</span>");
	$("#dIncomeTd").html("<span style='color:#caca00'><b>Manual Check Required</b></span>");
}
else if(i<2166){
	$("#dIncomeTitle").html("Disposable Income - <span style='color:red'>Support Required</span>");
	$("#dIncomeTd").html("<span style='color:red'><b>Support Required</b></span>");
}
else{
	if(dincome>=500){
		$("#dIncomeTitle").html("Disposable Income - <span style='color:green'>Pass</span>");
		$("#dIncomeTd").html("<span style='color:green'><b>Pass</b></span>");
	}
	if(dincome>(-1000) && dincome <500){
		$("#dIncomeTitle").html("Disposable Income - <span style='color:red'>Support Required</span>");
		$("#dIncomeTd").html("<span style='color:red'><b>Support Required</b></span>");
	}
}
//special rule
if( (dincome <= -(i*2)) && i>=2166 ){
	$("#dIncomeTitle").html("Disposable Income - <span style='color:#caca00'>Manual Check Required</span>");
	$("#dIncomeTd").html("<span style='color:#caca00'><b>Manual Check Required</b></span>");
	$("#diReason").append("Unreasonable expenses");
}

///////////////////////////////////////////////////calculate asset/////////////////////////////////////////////////////////////////
	
if( $("#homeValue").text()=="" && $("#motorFinance").text()!="No" && $("#motorFinance").text()!=""){ 
	$("#assetTitle").html("Asset - <span style='color:red'>Support Required</span>");	
	$("#assetTd").html("<span style='color:red'><b>Support Required</b></span>");
}
else{
	$("#assetTitle").html("Asset - <span style='color:green'>Pass</span>");	
	$("#assetTd").html("<span style='color:green'><b>Pass</b></span>");	
}
if($("#motor2Finance").text()=="No"){
	$("#assetTitle").html("Asset - <span style='color:green'>Pass</span>");	
	$("#assetTd").html("<span style='color:green'><b>Pass</b></span>");	
}

////////////////////////////////////////////////////calculate credit//////////////////////////////////////////////////////////////
var bname1=$("#bname1").text();
var wincome1=$("#workincome1").text();
var bname2=$("#bname2").text();
var wincome2=$("#workincome2").text();

var v1value=$("#vehicleValue").text(); v1value=v1value.replace(/\,/g,''); if(v1value=="") v1value=0;
var v2value=$("#vehicle2Value").text(); v2value=v2value.replace(/\,/g,''); if(v2value=="") v2value=0;
var v1finance=$("#motorFinance").text();
var v2finance=$("#motor2Finance").text();
var hvalue=$("#homeValue").text(); hvalue=hvalue.replace(/\,/g,''); if(hvalue=="") hvalue=0;

var pp1="Pass";
var ch1=$("#chistory1").text();
var gambling1=$("#gambling1").text();
var hardship1=$("#hardship1").text();
var mincome1=$("#mincome1").text(); mincome1=mincome1.replace(/\s/g, "");

var pp2="Fail";
var ch2=$("#chistory2").text();
var gambling2=$("#gambling2").text();
var hardship2=$("#hardship2").text();
var mincome2=$("#mincome2").text(); mincome2=mincome2.replace(/\s/g, "");

//////////pp1 creidt
var p1ds1=$("#p1ds1").text();
var p1ds2=$("#p1ds2").text();
var p1da1=$("#p1da1").text(); p1da1 = toNumber(p1da1);
var p1da2=$("#p1da2").text(); p1da2 = toNumber(p1da2);

if(ch1!="Clean (no default)" && ch1!="1 default" && ch1!="2 defaults") pp1="Fail";
if( gambling1=="Yes" || hardship1=="Yes") pp1="Fail";
if(mincome1=="Centrelink" && bname1=="" && wincome1=="" && ch1!="Clean (no default)") pp1="Fail";



if( bname1!="" && wincome1!=""  && ((v1finance=="No" && v1value <10000) || (hvalue==0 && (v1finance!="No" || v1value==0)) ) ){
	if(ch1=="1 default" || ch1=="2 defaults" ){
		var totalDA=0;
		if(p1ds1=="Paying" || p1ds1=="Outstanding") totalDA += p1da1;  //alert (totalDA+p1da1);
		if(p1ds2=="Paying" || p1ds2=="Outstanding") totalDA += p1da2; 
		if(totalDA>2000) pp1="Fail";	
	}
}
if( bname1!="" && wincome1!="" && ((v1finance=="No" && v1value >=10000) || hvalue>0) ){
	if(ch1=="2 defaults"){
		var totalDA=0;
		if(p1ds1=="Paying" || p1ds1=="Outstanding") totalDA += p1da1;
		if(p1ds2=="Paying" || p1ds2=="Outstanding") totalDA += p1da2;
		if(totalDA>5000) pp1="Fail"; 
			
	}
	if(ch1=="1 default"){
		var totalDA=0;
		if(p1ds1=="Paying" || p1ds1=="Outstanding") totalDA = p1da1;
		if(totalDA>10000) pp1="Fail"; 
	}
} 
////////pp1 fail, check with pp2
	if(pp1=="Fail" && $("#jointApplicant").text()=="No" ){
		pp2="Pass";
		var totalDA=0;
		var p2ds1=$("#p2ds1").text();
		var p2ds2=$("#p2ds2").text();
		var p2da1=$("#p2da1").text(); p2da1 = p2da1.replace(/\,/g,''); p2da1=parseInt(p2da1);
		var p2da2=$("#p2da2").text(); p2da2 = p2da2.replace(/\,/g,''); p2da2=parseInt(p2da2);
		
		if(gambling2=="Yes") pp2="Fail"; 
		if(hardship2=="Yes") pp2="Fail"; 
		if(ch2!="Clean (no default)" && ch2!="1 default" && ch2!="2 defaults") pp2="Fail"; 	
		if(mincome2=="Centrelink" && bname2=="" && wincome2=="" && ch2!="Clean (no default)") pp2="Fail"; 
	
		if( bname2!="" && wincome2!=""  && ((v2finance=="No" && v2value <10000) || (hvalue==0 && (v2finance!="No" || v2value==0)) ) ){
			if(ch2=="1 default" || ch2=="2 defaults"){		
				if(p2ds1=="Paying" || p2ds1=="Outstanding") totalDA += p2da1;
				if(p2ds2=="Paying" || p2ds2=="Outstanding") totalDA += p2da2;
				if(totalDA>2000) pp2="Fail"; 	
			}
		}
		if( bname2!="" && wincome2!=""  && ((v2finance=="No" && v2value >=10000) || hvalue>0) ){
			if(ch2=="2 defaults"){
				var totalDA=0;
				if(p2ds1=="Paying" || p2ds1=="Outstanding") totalDA += p2da1;
				if(p2ds2=="Paying" || p2ds2=="Outstanding") totalDA += p2da2;
				if(totalDA>5000) pp2="Fail"; 
			}
			if(ch2=="1 default"){
				var totalDA=0;
				if(p2ds1=="Paying" || p2ds1=="Outstanding") totalDA = p2da1;
				if(totalDA>10000) pp2="Fail"; 
			}
		}
	}


if(pp1=="Fail" && pp2=="Fail"){
	$("#creditTitle").html("Credit - <span style='color:red'>Support Required</span>");	
	$("#creditTd").html("<span style='color:red'><b>Support Required</b></span>");	
}
else{
	$("#creditTitle").html("Credit - <span style='color:green'>Pass</span>");	
	$("#creditTd").html("<span style='color:green'><b>Pass</b></span>");	
}
if(hardship1=="Yes" && hardship2=="Yes") {
	$("#creditTitle").html("Credit - <span style='color:red'>Declined</span>");	
	$("#creditTd").html("<span style='color:red'><b>Declined</b></span>");	
}
if(mincome1=="Centrelink" && bname1=="" && $("#noa").text()=="One" && ch1!="Clean (no default)"){
	$("#creditTitle").html("Credit - <span style='color:red'>Support Required</span>");	
	$("#creditTd").html("<span style='color:red'><b>Support Required</b></span>");	
	$("#dIncomeTitle").html("Disposable Income - <span style='color:red'>Support Required</span>");
	$("#dIncomeTd").html("<span style='color:red'><b>Support Required</b></span>");	
	$("#diReason").append("Marginal case as Centrelink income only and bad credit");
}


///////////////////////////////////////////////// calculate estimate approval//////////////////////////////////////////////
//var temp=$("#homeValue").text();
if($("#homeValue").text()!="" && $("#homeValue").text()!=0){
	$("#estimateApp").text("Property - up to $9,000");
}
else if($("#vehicleValue").text()!="" && $("#vehicleValue").text()!=0 ){
	var d=$("#desiredAmount").text(); d = d.replace(/\,/g,'');
	var n=$("#vehicleValue").text(); n = n.replace(/\,/g,'');
	//alert (n);
	if(n<=3000){
		if(d>1000)
			$("#estimateApp").text("Vehicle: $1000-$2000");
		else
			$("#estimateApp").text("Vehicle: $"+d);
		if(d>=4000)
			$("#ea").text("$1000-$2000");
	}
	else if(n<10000){
		n=n/2;
		var minValue=Math.round((n-1000)/1000)*1000; if(minValue<1000) minValue=1000;
		var maxValue=Math.round((n+1000)/1000)*1000;
		if(d>minValue)
			$("#estimateApp").text("Vehicle: $" + minValue + "-$" + maxValue);
		else
			$("#estimateApp").text("Vehicle: $"+d);	
		if((d-maxValue)>=2000)	
			$("#ea").text("$" + minValue + "-$" + maxValue);	
	}
	else{
		if(d>5000)
			$("#estimateApp").text("Vehicle: $5000");
		else
			$("#estimateApp").text("Vehicle: $"+d);
		if(d>=7000)
			$("#ea").text("$5000");	
	}
}
else{  //no real estate, no vehicle
	$("#estimateApp").text("None");
	$("#assetTitle").html("Asset - <span style='color:red'>Support Required</span>");	
	$("#assetTd").html("<span style='color:red'><b>Support Required</b></span>");
}

////////////business purpose, disposable income always pass////////////////////
if($("#psummary").text()=="Business"){
	$("#dIncomeTitle").html("Disposable Income - <span style='color:green'>Pass</span>");
	$("#dIncomeTd").html("<span style='color:green'><b>Pass</b></span>");
}


///////////////////////////////////////show hide add on applicant/////////////////////////////////////
if($("#jointApplicant").text()=="Yes"){
	$("#jointApplicantName").show();
	$("#jointApplicant").hide();
}else{
	$("#jointApplicantName").hide();
	$("#jointApplicant").hide();
}


if($("#preapp_purpose").text()=="Personal"){
	if($("#preapp_chistory").text()!="Clean (no default)" && $("#preapp_chistory").text()!="1 default"){
		$("#preapp_result").text("apjoin.htm");
	}
}

if($("#dIncomeTitle")=="Manual Respond")
	$("#estimateApp").text("N/A");

});

function toNumber(str){
	if(str=="")
		str=0;
	else{
		str=str.replace(/\,/g,'');
		str=parseInt(str);
	}
	return str;	
}